﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Streambolics.Data;

namespace Ewma
{
    /// <summary>
    ///     The base class for every webpage that knows about users
    /// </summary>
    
    public class UserWebPage : CommonWebPage
    {
        private User _User;
        private User _Owner;
        private IHasOwner _Object;

        private static User _UnknownUser;

        private User GetUserFromSession ()
        {
            return null;
        }

        private User GetUserFromMembership ()
        {
            return null;
        }

        private User GetUserFromLoginToken ()
        {
            return null;
        }

        private void SaveUserToSession (User aUser)
        {
        }

        private User GetUnknownUser ()
        {
            //  Note that there is a race condition since multiple threads may create the unknown user.
            //  I decided not to bother.

            if (_UnknownUser == null)
            {
                _UnknownUser = new User
                {
                    Name = "Unknown"
                };
            }
            return _UnknownUser;
        }

        /// <summary>
        ///     The logged-in user
        /// </summary>
        /// <returns></returns>
        User GetUser ()
        {
            if (_User == null)
            {
                _User = GetUserFromSession ();
                if (_User == null)
                {
                    _User = GetUserFromMembership ();

                    if (_User == null)
                    {
                        _User = GetUserFromLoginToken ();
                    }

                    if (_User == null)
                    {
                        _User = GetOwner ();
                        if (_User.IsProtected)
                        {
                            _User = null;
                        }
                    }

                    if (_User == null)
                    {
                        _User = GetUnknownUser ();
                    }
                    else
                    {
                        SaveUserToSession (_User);
                    }
                }
            }
            return _User;
        }

        /// <summary>
        ///     The owner of the current object
        /// </summary>
        /// <returns></returns>
        public User GetOwner ()
        {
            if (_Owner == null)
            {
                IHasOwner o = GetObject ();
                if (o != null)
                {
                    _Owner = o.Owner;
                }
            }
            return _Owner;
        }

        /// <summary>
        ///     The current object
        /// </summary>
        /// <returns></returns>
        IHasOwner GetObject ()
        {
            if (_Object == null)
            {
            }
            return _Object;
        }

        bool IsAuthorized
        {
            get
            {
                return GetUser () == GetOwner ();
            }
        }
    }
}